package com.rxy.seckill.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rxy.seckill.redis.RedisService;
import com.rxy.seckill.result.Result;
import com.rxy.seckill.service.MiaoshaUserService;
import com.rxy.seckill.vo.LoginVo;

@Controller
@RequestMapping("/login")
public class LoginController {

	private static Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	RedisService redisService;
	
	@Autowired
	MiaoshaUserService userService;
	
	//跳转到登录页
    @GetMapping("/to_login")
    public String toLogin() {
        return "login";
    }
    
    //登录逻辑处理
    @PostMapping("/do_login")
    @ResponseBody
    public Result<Boolean> doLogin(HttpServletResponse response, @Valid LoginVo loginVo) {
    	log.info(loginVo.toString());
    	//登录
    	userService.login(response,loginVo);
    	//由于false的情况不存在，错误都在全局异常中进行处理了，所以这里直接返回true
    	return Result.success(true);
    }
}
